/**
 * Copyright © 2018, TaoDing
 * <p>
 * All Rights Reserved.
 */

/**
 * 类功能描述
 *
 * @author Leon
 * @version 2018/11/7 16:29
 */
public class NumArray {

    private SegmentTree<Integer> segmentTree;

    public NumArray(int[] nums) {
        Integer[] data = new Integer[nums.length];
        if (nums.length > 0) {
            for (int i = 0; i < nums.length; i++) {
                data[i] = nums[i];
            }
            segmentTree = new SegmentTree<>(data, (a, b) -> a + b);
        }
    }

    public int sumRange(int i, int j) {
        if (segmentTree == null) {
            throw new IllegalArgumentException("Segment Tree Is Null.");
        }
        return segmentTree.query(i, j);
    }

    @FunctionalInterface
    public interface Merger<E> {

        E merge(E a, E b);
    }

    public class SegmentTree<E> {

        private E[] tree;
        private E[] data;
        private Merger<E> merger;

        public SegmentTree(E[] arr, Merger<E> merger) {
            this.merger = merger;

            data = (E[])new Object[arr.length];
            for (int i = 0; i < arr.length; i++) {
                data[i] = arr[i];
            }

            tree = (E[])new Object[4 * arr.length];
            buildSegmentTree(0, 0, arr.length - 1);
        }

        /**
         * 在treeIndex的位置创建[l,r]的线段树
         *
         * @param treeIndex
         * @param l
         * @param r
         */
        private void buildSegmentTree(int treeIndex, int l, int r) {
            // The entrance of recursion
            if (l == r) {
                tree[treeIndex] = data[l];
                return;
            }
            int leftTreeIndex = leftChild(treeIndex);
            int rightTreeIndex = rightChild(treeIndex);
            // Avoid integer leak
            int mid = l + (r - l) / 2;
            buildSegmentTree(leftTreeIndex, l, mid);
            buildSegmentTree(rightTreeIndex, mid + 1, r);

            tree[treeIndex] = merger.merge(tree[leftTreeIndex], tree[rightTreeIndex]);
        }

        /**
         * 返回区间[queryL, queryR]的值
         *
         * @param queryL
         * @param queryR
         * @return
         */
        public E query(int queryL, int queryR) {
            if (queryL < 0 || queryL >= data.length || queryR < 0 ||
                    queryR >= data.length || queryL > queryR) {
                throw new IllegalArgumentException("Index is illegal.");
            }
            return query(0, 0, data.length -1, queryL, queryR);
        }

        /**
         * 在treeIndex为根的线段树中[l,r]的区间内，搜索区间[queryL, queryR]的值
         *
         * @param treeIndex
         * @param l
         * @param r
         * @param queryL
         * @param queryR
         * @return
         */
        private E query(int treeIndex, int l, int r, int queryL, int queryR) {
            if (l == queryL && r == queryR) {
                return tree[treeIndex];
            }
            int mid = l + (r - l) / 2;
            int leftChildIndex = leftChild(treeIndex);
            int rightChildIndex = rightChild(treeIndex);
            if (queryL >= mid + 1) {
                return query(rightChildIndex, mid + 1, r, queryL, queryR);
            } else if (queryR < mid + 1) {
                return query(leftChildIndex, l, mid, queryL, queryR);
            }
            E leftResult = query(leftChildIndex, l, mid, queryL, mid);
            E rightResult = query(rightChildIndex, mid + 1, r, mid + 1, queryR);
            return merger.merge(leftResult, rightResult);
        }

        public int getSize() {
            return data.length;
        }

        public E get(int index) {
            if (index < 0 || index >= data.length) {
                throw new IllegalArgumentException("Index is illegal.");
            }
            return data[index];
        }

        /**
         * 返回完全二叉树数组表示中，一个索引所在元素的左孩子的索引
         *
         * @param index
         * @return
         */
        public int leftChild(int index) {
            return index * 2 + 1;
        }

        /**
         * 返回完全二叉树数组表示中，一个索引所在元素的右孩子的索引
         *
         * @param index
         * @return
         */
        public int rightChild(int index) {
            return index * 2 + 2;
        }

        @Override
        public String toString(){
            StringBuilder res = new StringBuilder();
            res.append('[');
            for (int i = 0 ; i < tree.length ; i ++) {
                if (tree[i] != null) {
                    res.append(tree[i]);
                } else {
                    res.append("null");
                }
                if (i != tree.length - 1) {
                    res.append(", ");
                }
            }
            res.append(']');
            return res.toString();
        }
    }

    public static void main(String[] args) {
        int[] ns = {2,3,54, -9, 2, 34, 89, 19, -98};
        NumArray a = new NumArray(ns);
        System.out.println(a.sumRange(0, 2));
    }
}
